package com.engine.modules.ytht.service;

import com.engine.modules.ytht.entity.FarmersFieldsEntity;
import com.engine.modules.ytht.entity.PlotInfoEntity;
import com.engine.modules.ytht.mapper.FarmersBindingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName FarmersFieldBinding.java
 * @Description TODO
 * @createTime 2021年12月17日
 */
@Slf4j
@Service
public class FarmersFieldBindingImpl implements FarmersFieldBinding{

    @Autowired
    FarmersBindingMapper farmersBindingMapper;

    @Autowired
    FieldBlockBindingService fieldBlockBindingService;


    @Transactional
    public List<String> updateByFarmersFieldsEntity(FarmersFieldsEntity farmersFieldsEntity){
        List<String> rslist=new ArrayList<>();
        for (String field_id:farmersFieldsEntity.getFieldId()) {
            PlotInfoEntity plotInfo = new PlotInfoEntity();
            plotInfo.setField_black_no(field_id);
            plotInfo.setSuoding(Long.valueOf(1));
            try{
                String rs1 = fieldBlockBindingService.UpdateGroupGeneralDataByEntity(plotInfo, "b_gis_polygon_qqcg_x");
                String rs2 = farmersBindingMapper.updateByFarmersFieldsEntity(
                        farmersFieldsEntity.getIdCard(), farmersFieldsEntity.getYear(), field_id);
                rslist.add(rs1);
            }
            catch (Exception e){
                rslist.add("id："+field_id+",错误:"+e.getMessage());
            }
        }
        return rslist;
    }

    @Transactional
    public List<String> deleteByFarmersFields(FarmersFieldsEntity farmersFieldsEntity) {
        if(farmersFieldsEntity.getFieldId().size()==0)
            farmersFieldsEntity.setFieldId(farmersBindingMapper.seleteFarmersFieldsEntityByIdCard(farmersFieldsEntity.getIdCard(),farmersFieldsEntity.getYear()).getFieldId());
        List<String> rslist = new ArrayList<>();
        for (String field_id : farmersFieldsEntity.getFieldId()) {
            int x=farmersBindingMapper.deleteByFarmersFieldsEntity(farmersFieldsEntity.getIdCard(), farmersFieldsEntity.getYear(), field_id);
            if(x==0)
                return rslist;
            if (farmersBindingMapper.selectBundingCountByFieldId(field_id) == 0) {
                PlotInfoEntity plotInfo = new PlotInfoEntity();
                plotInfo.setField_black_no(field_id);
                plotInfo.setSuoding(Long.valueOf(0));
                try {
                    String rs1 = fieldBlockBindingService.UpdateGroupGeneralDataByEntity(plotInfo, "b_gis_polygon_qqcg_x");
                } catch (Exception e) {
                    rslist.add("id："+field_id+",错误:"+e.getMessage());
                }
            }
            rslist.add(field_id);
        }
        return rslist;
    }

    public FarmersFieldsEntity seleteFarmersFieldsEntityByIdCard(String id_card,Long year) {
        return farmersBindingMapper.seleteFarmersFieldsEntityByIdCard(id_card,year);
    }
}
